package com.simple.util.security;

import com.simple.util.base.ByteUtil;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

/**
 * @program: simple_tools
 * @description: DES加密
 * @author: Mr.chen
 * @create: 2020-06-08 16:07
 **/
public class DESEncrypt {
    private static String Algorithm = "DES";

    /**
     * 加密以byte[]明文输入,byte[]密文输出
     *
     * @param byteS
     * @param key
     * @return
     */
    private static byte[] getEncCode(byte[] byteS, Key key) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     * 解密以byte[]密文输入,以byte[]明文输出
     *
     * @param byteD
     * @return
     */
    private static byte[] getDesCode(byte[] byteD, Key key) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     * 加密String明文输入,String密文输出
     *
     * @param strEnc
     * @param key
     * @return
     */
    public static String encrypt(String strEnc, Key key) {
        String strMi = "";
        try {
            return ByteUtil.byteArrayToHexString(getEncCode(strEnc.getBytes(), key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMi;
    }

    /**
     * 解密以String密文输入,String明文输出
     *
     * @param strEnc
     * @param key
     * @return
     */
    public static String dencrypt(String strEnc, Key key) {
        String strMing = "";
        try {
            return new String(getDesCode(ByteUtil.fromHexString(strEnc), key));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMing;
    }

    /**
     * 根据参数生成KEY
     *
     * @param strKey
     * @return
     */
    public static Key getKey(String strKey) {
        Key key = null;
        try {
            KeyGenerator _generator = KeyGenerator.getInstance(Algorithm);
            _generator.init(new SecureRandom(strKey.getBytes()));
            key = _generator.generateKey();
            _generator = null;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return key;
    }

    public static void main(String[] args) {
        System.out.println("hello");
        Key key = DESEncrypt.getKey("secureKey");// 生成密匙
        String strEnc = DESEncrypt.encrypt("123456:123456:asddffgghhjjkkkhkhkhkjhkhkhkhkhkhkh:201265656565", key);// 加密字符串,返回String的密文
        System.out.println(strEnc);
        Key key2 = DESEncrypt.getKey("secureKey");// 生成密匙
        String strDes = DESEncrypt.dencrypt(strEnc, key2);// 把String类型的密文解密
        System.out.println(strDes);
    }
}
